//
//  SEGActivityCollectUploadViewModel.m
//  Commom
//
//  Created by 梁学彰 on 2020/6/19.
//

#import "SEGActivityCollectUploadViewModel.h"

@interface SEGActivityCollectUploadViewModel ()
/**
 活动ID
 */
@property (nonatomic, strong)NSString *actId;

@end

@implementation SEGActivityCollectUploadViewModel
#pragma mark - life cycle
- (instancetype)initWithActId:(NSString *)actId isReview:(BOOL)isReview {
    if (self = [super init]) {
        self.actId = actId;
        self.isReview = isReview;
        [self initData];
        [self seg_creatSaveCommentCommand];
    }
    return self;
}

- (void)dealloc {
    
}

#pragma mark - init

- (void)initData {
    self.savePicArray = [[NSMutableArray alloc] init];
    self.saveUrlArray = [[NSMutableArray alloc] init];
}

#pragma mark - send request
- (void)seg_creatSaveCommentCommand {
    @weakify(self);
    self.upLoadImagesAndSaveCommentCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        @strongify(self);
        RACTupleUnpack(NSString *content,NSString *show) = input;
        
        return [RACSignal concat:@[[self checkParamsWithContent:content],[self upLoadImages],[self saveCommentWithContent:content show:show]]];
    }];
}
#pragma mark - custom delegate

#pragma mark - system delegate

#pragma mark - event response

#pragma mark - private methods
- (RACSignal *)checkParamsWithContent:(NSString *)content {
    @weakify(self);
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        
        if (content.length == 0) {
            [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:@"请填写内容"}]];
            return nil;
        }
        
        if (content.length > 140) {
            [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:@"亲，您输入的内容已超过140字。"}]];
            return nil;
        }
        
        if (self.savePicArray.count == 0)  {
            [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:@"亲，没有上传图片，请上传图片后再确定提交！"}]];
            return nil;
        }
        
        [subscriber sendCompleted];
        
        return nil;
    }];
    return signal;
}

- (RACSignal *)upLoadImages {
    @weakify(self);
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        
        NSArray *uploadArr = [self.savePicArray subarrayWithRange:NSMakeRange(self.saveUrlArray.count, self.savePicArray.count - self.saveUrlArray.count)];
        if(uploadArr.count > 0) {
            
            [subscriber sendNext:@(1)];
            [[SEGMultipartfileuploadManager sharedManager] requestMultipartfileuploadWithImage:uploadArr progress:^(double progressValue) {
                
            } completed:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                
                if(isSuccessed) {
                    NSArray *data = result[@"data"];
                    //存储图片链接
                    [self.saveUrlArray addObjectsFromArray:data];
                    
                    [subscriber sendCompleted];
                }else {
                    [subscriber sendNext:@(0)];
                    [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:errorMessage}]];
                }
            }];
        }else {
            [subscriber sendCompleted];
        }
        return nil;
    }];
    return signal;
}

- (RACSignal *)saveCommentWithContent:(NSString *)content show:(NSString *)show {
    @weakify(self);
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        
        NSString *quizPic = [self.saveUrlArray componentsJoinedByString:@","];
        
        [self.activityManager requestCommentActivityWithObjId:self.actId quizContent:content quizPic:quizPic show:show finished:^(BOOL isSuccessed, id result, NSString* codeType, NSString* errorMessage) {
            [subscriber sendNext:@(0)];
            if (isSuccessed) {
                [subscriber sendCompleted];
                // 参加征集活动成功 触发行为积分
                [[SEGAnalyseBehaviorManager sharedManager] requestIntegralGrowthWithCode:SEGIntegralCode.participateActivity module:SEGIntegralModule.activityDetail businessID:self.actId finished:nil];
            }else {
                [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:codeType.integerValue userInfo:@{NSLocalizedDescriptionKey:errorMessage}]];
            }
        }];
        
        return nil;
    }];
    return signal;
}


#pragma mark - setter and getter
- (SEGActivityManager *)activityManager {
    if (!_activityManager) {
        _activityManager = [[SEGActivityManager alloc] init];
    }
    return _activityManager;
}
@end
